<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validate-trigger="submit" err-show-type="toast">
      <uni-forms-item name="tripType" label="行程类型" required>
        <uni-data-checkbox v-model="formData.tripType" :localdata="formOptions.tripType_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="startLc" label="起点" required>
        <uni-easyinput v-model="formData.startLc"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="endLc" label="终点" required>
        <uni-easyinput v-model="formData.endLc"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="startTs" label="出发开始时间" required>
        <uni-datetime-picker return-type="timestamp" v-model="formData.startTs"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="endTs" label="出发截止时间" required>
        <uni-datetime-picker return-type="timestamp" v-model="formData.endTs"></uni-datetime-picker>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" @click="submit">提交</button>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/trip.js';

  const db = uniCloud.database();
  const dbCollectionName = 'trip';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.indexOf(key) > -1) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "tripType": 0,
        "startLc": "增城区凤岗地铁站C口",
        "endLc": "天河区凯华国际东门",
        "startTs": null,
        "endTs": null
      }
      return {
        formData,
        formOptions: {
          "tripType_localdata": [
            {
              "text": "车找人",
              "value": 0
            },
            {
              "text": "人找车",
              "value": 1
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onLoad(e) {
      if (e.id) {
        const id = e.id
        this.formDataId = id
        this.getDetail(id)
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
          uni.showToast({
            icon: 'none',
            title: '修改成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
        db.collection(dbCollectionName).doc(id).field("tripType,startLc,endLc,startTs,endTs").get().then((res) => {
          const data = res.result.data[0]
          if (data) {
            this.formData = data
            
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>

<style>
  .uni-container {
    padding: 15px;
  }

  .uni-input-border,
  .uni-textarea-border {
    width: 100%;
    font-size: 14px;
    color: #666;
    border: 1px #e5e5e5 solid;
    border-radius: 5px;
    box-sizing: border-box;
  }

  .uni-input-border {
    padding: 0 10px;
    height: 35px;

  }

  .uni-textarea-border {
    padding: 10px;
    height: 80px;
  }

  .uni-button-group {
    margin-top: 50px;
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    justify-content: center;
  }

  .uni-button {
    width: 184px;
  }
</style>
